<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>分配角色权限</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="${ctx}/rs/js/zTree/css/zTreeStyle/metro.css"/>
<script src="${ctx}/rs/js/zTree/js/jquery.ztree.all-3.5.min.js"></script>
</head>
<body>
<form id="form1" class="form-horizontal">
	  <input type="hidden" id="weight" name="weight" value="${weight}" />
      <div class="form-group">
         <input type="hidden" id="permissionIds" name="permissionIds"/>
         <div class="perm"><ul id="tree" class="ztree"></ul></div>
      </div>
	   
      <div class="form-group">
        <div class="col-sm-10 text-center pt15">
		 <shiro:hasPermission name="allot:save">
		     <button type="button" class="btn btn-primary btn-lg srt-btn active mr30" onclick="saveData()">保存</button>
          </shiro:hasPermission>
        </div>
      </div>

      <div class="form-group">
        <div class=" col-sm-10">
          <span id="error-text" style="color: #FF0000;"></span>
        </div>
      </div>
</form>
<script type="text/javascript">
    var setting = {
	    check: {
	        enable: true
	    },
	    view: {
	        dblClickExpand: false,
	        showLine: true,
	        selectedMulti: false
	    },
	    data: {
	    	key: {
//				children: "permissions",
				name: "permName",
				url: "#"
			},
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "parentId"
			}
	    },
	    callback: {
	     	onCheck: onCheck
	    }
	};
    
    $.ajax({
        url: "${ctx}/permission/tree",
        success: function (data) {
        	var zNodes = eval(data);
        	var treeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
        	var array = eval('${permIds}');
        	var v ="";
        	for (var i = 0, l = array.length; i < l; i++) {
        		var node = treeObj.getNodeByParam("id", array[i], null);
        		if(node){
        			v += node.id + ",";
            		treeObj.checkNode(node, true, false);
        		}
			}
        	$("#permissionIds").val(v);
        	
        },
        error: function () {
        	bootstrapQ.msg({
    			    msg  : '树状下拉列表加载失败！',
    			    type : 'danger',
    			    time : 2000
    			});
        }
    });
    
    function onCheck(e, treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("tree"),
		nodes = zTree.getCheckedNodes(true),
		v = "";
		for (var i=0, l=nodes.length; i<l; i++) {
			v += nodes[i].id + ",";
		}
		if (v.length > 0 ) v = v.substring(0, v.length-1);
		$("#permissionIds").val(v);
	}
    
    function saveData() {
    	var permIds = $("#permissionIds").val();
//     	if(permIds){
    		$.ajax({
    	           url: "${ctx}/permission/saveRolePermTree.htm?roleIds="+'${roleIds}'.split(",")+"&permissionIds="+permIds.split(","),
    	           type: "post",
    	           success: function (text) {
    	        	   // 普通消息提示条
    	        	   bootstrapQ.msg({
    	        		   msg : '分配角色权限成功（注意：该功能需重启才生效）！',
   						   type : 'success',
   						   time : 2000
   					   });
   					   //触发隐藏确认的按钮，引用回调方法关闭窗口并刷新列表
   					   $("button.bsok").click();
    	           }
    	       });
//     	}else{
//     		bootstrapQ.msg({
// 			    msg  : '请选择分配权限！',
// 			    type : 'info',
// 			    time : 2000
// 			});
//     	}
	}
</script> 
</body>
</html>